package com.qqgame.hlddz;

import android.app.ActivityOptions;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class NaviActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        ActivitySettingsBinding binding = ActivitySettingsBinding.inflate(getLayoutInflater());
//        setContentView(binding.getRoot());
//
//        showData();
//
//        binding.save.setOnClickListener(view -> saveData());

        Intent intent = getIntent();

//        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Uri data = intent.getData();

            String lat = data.getQueryParameter("lat");
            String lon = data.getQueryParameter("lon");
            String poiName = data.getQueryParameter("poiname");

            SharedPreferences sharedPreferences = getSharedPreferences("MyPreferences", MODE_PRIVATE);
            String naviAppValue = sharedPreferences.getString("naviApp", "1");

            if ("1".equals(naviAppValue)) {
                gaoDeNavi(lat, lon);
            }

            if ("2".equals(naviAppValue)) {
                baiduNavi(lat, lon, poiName);
            }

            if ("3".equals(naviAppValue)) {
                baiduNavi(lat, lon, poiName);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                gaoDeNavi(lat, lon);
            }

//        }

        finish();


    }

    private void gaoDeNavi(String lat, String lon) {
        try {
//            ActivityOptions options = ActivityOptions.makeBasic();
            //这里一直display0是第一块屏；display1是第二块屏
//            options.setLaunchDisplayId(MainActivity.display);
            Intent secondIntent = new Intent();
            ComponentName cn = new ComponentName("com.autonavi.amapautp", "com.autonavi.amapauto.MainMapActivity");
            secondIntent.setComponent(cn);
            //该句很重要，不添加则无法推送到副屏
            secondIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            secondIntent.setData(Uri.parse("androidauto://navi?sourceApplication=Speech&dev=0&poiname=lip&lat=" + lat + "&lon=" + lon));
//            secondIntent.setData(Uri.parse("androidauto://rootmap"));
            // 使用ActivityOptions指定显示屏幕为Intent增加标志Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_NEW_TASK
//            startActivity(secondIntent, options.toBundle());
            startActivity(secondIntent);
        } catch (Exception e) {
            Toast.makeText(this, "e：" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    private void baiduNavi(String lat, String lon, String poiName) {
        try {
            Intent secondIntent = new Intent();
            secondIntent.setData(Uri.parse("baidumap://map/navi?location=" + lat + "," + lon + "&coord_type=gcj02&query=" + poiName + "&src=webapp.companyName.appName"));
            startActivity(secondIntent);
        } catch (Exception e) {
            Toast.makeText(this, "e：" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }


}